package server;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

// 自定义类加载器，加载外部的servlet类
public class MyClassLoader extends ClassLoader {

    /**
     * 通过路径以及名称找到class对象
     */
    protected Class<?> findClass(String basePath, String name) {
        try {
            String myPath = basePath.replaceAll("\\\\", "/") + name.replaceAll("\\.", "/") + ".class";
            Path path = Paths.get(myPath);
            byte[] cLassBytes = Files.readAllBytes(path);
            return defineClass(name, cLassBytes, 0, cLassBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}